from state.player import Player
from state.block import WorldBlock, ZoningBlock

import random
from typing import Dict, Tuple

from state.reference import BLOCK_ENV_LIST, BlockEnvWidget


# 初始化世界地图
def initial_world(world_size) -> Dict[Tuple[int, int], WorldBlock]:
    # 按权重获取地块环境值
    environments = random.choices(BLOCK_ENV_LIST, weights=BlockEnvWidget, k=world_size * world_size)
    return {
        (i, j): WorldBlock(i, j, environments[i * world_size + j])
        for i in range(world_size) for j in range(world_size)
    }


def initial_player(blocks: Dict[Tuple[int, int], WorldBlock], name: str) -> Player:
    bs = list()
    for block in (b for b in blocks.values() if b.env == 0):
        for neighbor in block.neighbors(blocks):
            if neighbor.player is None:
                bs.append(block)

    selected_block: WorldBlock = random.choice(bs)
    player = Player(name)
    player.discover_block(selected_block)
    player.add_block(selected_block)

    # 在该地块建立前哨战
    zoning: ZoningBlock = selected_block.zoning_map[(0, 0)]
    zoning.building = "Outputs"

    return player
